package com.ccgk.logistics.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ccgk.common.page.PaginationVo;
import com.ccgk.logistics.domain.Container;
import com.ccgk.logistics.domain.ContainerExample;
import com.ccgk.logistics.domain.Currency;
import com.ccgk.logistics.domain.Fee;
import com.ccgk.logistics.domain.FeeExample;
import com.ccgk.logistics.persistence.FeeMapper;
import com.ccgk.logistics.service.FeeService;
@Service
public class FeeServiceImpl implements FeeService{
	
	@Autowired
	private FeeMapper feeMapper;
	
	@Override
	public Integer add(Fee fee) {
		return feeMapper.insert(fee);
	}

	@Override
	public Integer delete(int id) {
		return feeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Fee> findAll() {
		return feeMapper.selectByExample(null);
	}

	@Override
	public Fee getById(int id) {
		return feeMapper.selectByPrimaryKey(id);
	}

	@Override
	public Fee getByName(String name){
		if(StringUtils.isBlank(name))
			return null;
		FeeExample example = new FeeExample();
		example.createCriteria().andNameEqualTo(name);
		List<Fee> result = feeMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		return result.get(0);
	}
	
	@Override
	public Integer update(Fee fee) {
		return feeMapper.updateByPrimaryKey(fee);
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id:idIntArray){
			delete(id);
		}
	}

	@Override
	public PaginationVo<Fee> findList(FeeCriteria criteria,int currentPage, int pageSize) {
		
		FeeExample example = new FeeExample();
		FeeExample.Criteria _criteria = example.createCriteria();
		Map<Integer,Object> params = criteria.getParams();
		for(Integer key : params.keySet()){
			if(key.equals(1)){
				_criteria.andNameLike(params.get(key).toString());
			}
		}
		example.setOrderByClause("name asc");
		List<Fee> data = feeMapper.selectByExample(example,new RowBounds((currentPage-1)*pageSize, pageSize));
		int recordCount = feeMapper.countByExample(example);
		PaginationVo<Fee> result = new PaginationVo<Fee>(data, recordCount, pageSize, currentPage);
		return result;
	}
	
}
